package org.jim.core.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import org.jim.core.ImConst;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @FileName:
 * @Description: 用于动态构建出redis查询的key
 * @Author linjie
 * @Date 2020/7/296:30 PM
 * @Version 4.0.0
 */
public class KeyUtil implements ImConst {

    private static final String SUFFIX = ":";

    private static final Pattern pattern = Pattern.compile("\\{(.*?)\\}");

    /**
     * 指定每个值的后缀
     *
     * @param template
     * @param params
     * @param suffix
     * @return
     */
    public static String formatKey(String template, Map<String, String> params, String suffix) {
        if (MapUtil.isNotEmpty(params)) {
            String val;
            Map<String, String> result = new HashMap<>();
            for (String key : params.keySet()) {
                val = params.get(key);
                result.put(key, StrUtil.isBlank(val) ? "" : val + suffix);
            }
            return StrUtil.removeSuffix(format(template, result), suffix);
        } else {
            return template;
        }

    }

    /**
     * 每个值的固定后缀为:
     *
     * @param template
     * @param params
     * @return
     */
    public static String formatKey(String template, Map<String, String> params) {
        return formatKey(template, params, SUFFIX);
    }

    /**
     * 每个值的固定后缀为:
     *
     * @param template
     * @param params   key,value,key,value
     * @return
     */
    public static String formatKey(String template, String... params) {
        Map<String, String> result = new HashMap<>();
        if (ArrayUtil.isNotEmpty(params)) {
            for (int i = 0; i < params.length; i = i + 2) {
                result.put(params[i], params[i + 1]);
            }
        }
        return formatKey(template, result, SUFFIX);
    }

    /**
     * 格式化字符串 字符串中使用{key}表示占位符 利用反射 自动获取对象属性值 (必须有get方法)
     *
     * @param sourStr 需要匹配的字符串
     * @param obj     参数集
     * @return
     */
    public static String format(String sourStr, Object obj) {
        if (obj == null) {
            return sourStr;
        }
        String tagerStr = sourStr;
        Matcher matcher = pattern.matcher(tagerStr);
        if (obj instanceof Map) {
            while (matcher.find()) {
                String key = matcher.group();
                String keyclone = key.substring(1, key.length() - 1).trim();
                Object value = ((Map) obj).get(keyclone);
                if (value != null)
                    tagerStr = tagerStr.replace(key, value.toString());
            }
        } else {
            PropertyDescriptor pd;
            Method getMethod;
            // 匹配{}中间的内容 包括括号
            while (matcher.find()) {
                String key = matcher.group();
                String keyclone = key.substring(1, key.length() - 1).trim();
                try {
                    Object value = ReflectUtil.getFieldValue(obj, keyclone);
                    if (value != null)
                        tagerStr = tagerStr.replace(key, value.toString());
                } catch (Exception e) {

                }
            }
        }
        return tagerStr;
    }
}
